package com.guaju.yueshangclothing.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.guaju.yueshangclothing.R;
import com.guaju.yueshangclothing.databinding.SearchbarBinding;
import com.guaju.yueshangclothing.viewmodel.SearchbarVM;

/**
 * Created by guaju on 2018/9/11.
 */

public class SearchBar extends FrameLayout {

    private LayoutInflater layoutInflater;
    private SearchbarBinding binding;
    private boolean cursor;

    public SearchBar(@NonNull Context context) {
        super(context);
        init();
        initData();
    }

    public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.searchbar_style);
        boolean backable = a.getBoolean(R.styleable.searchbar_style_backable, false);
        boolean msgable = a.getBoolean(R.styleable.searchbar_style_msgable, false);
        boolean showcancle = a.getBoolean(R.styleable.searchbar_style_showcancle, false);
        cursor = a.getBoolean(R.styleable.searchbar_style_cursor, true);
        binding.back.setVisibility(backable?View.VISIBLE:View.GONE);
        if (!backable){
            binding.space.setVisibility(View.VISIBLE);
        }else{
            binding.space.setVisibility(View.GONE);
        }
//        -----
        binding.msg.setVisibility(msgable?View.VISIBLE:View.GONE);
        if (msgable){
            binding.cancle.setVisibility(View.GONE);
        }

//        -----
        binding.cancle.setVisibility(showcancle?View.VISIBLE:View.GONE);
        if (showcancle){
            binding.msg.setVisibility(View.GONE);
        }
//        -----
        if (cursor){
            binding.searchEdittext.setVisibility(View.VISIBLE);
            binding.searchText.setVisibility(View.GONE);
        }else{
            binding.searchEdittext.setVisibility(View.GONE);
            binding.searchText.setVisibility(View.VISIBLE);
            binding.searchEdittext.requestFocus();
        }
        initData();


    }

    private void initData() {
        SearchbarVM searchbarVM = new SearchbarVM((Activity)(getContext()),cursor);
        binding.setViewmodel(searchbarVM);
    }

    private void init() {
        layoutInflater = LayoutInflater.from(getContext());
        binding = DataBindingUtil.inflate(layoutInflater, R.layout.searchbar, this, true);


    }




}
